package org.example;

import com.sun.org.apache.bcel.internal.generic.RET;

import java.util.Random;
import java.util.concurrent.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        method5();
    }


    public static void method1() {
        Object object = new Object();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while (true) {
                    synchronized (object) {

                        System.out.println("当前线程名字：" + Thread.currentThread().getName());
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }

                }
            }
        };

        int i = 0;
        while (i < 1000) {
            Thread thread1 = new Thread(runnable);
            thread1.setName(i + "");
            int min = 999;
            int max = 1000;
            int randomNumber = new Random().nextInt(max - min + 1) + min;
            thread1.setPriority(1000 / randomNumber);
            thread1.start();
            i++;
        }
    }


    static class Person {
        public int age;

    }

    public static void method2() {
        Lock lock = new ReentrantLock();
        Person person = new Person();
        Runnable runnable = () -> {
            while (true) {
                try {
                    lock.lock();
                    person.age++;
                    System.out.println(person.age);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    lock.unlock();
                }
            }
        };

        int i = 0;
        while (i < 1000) {
            Thread thread1 = new Thread(runnable);
            thread1.setName(i + "");
            int min = 999;
            int max = 1000;
            int randomNumber = new Random().nextInt(max - min + 1) + min;
            thread1.setPriority(1000 / randomNumber);
            thread1.start();
            i++;
        }
    }


    public static void method3() {
        Person a = new Person();
        a.age = 5;

        new ThreadDemo(a).start();
        new ThreadDemo2(a).start();


    }

    static class ThreadDemo extends Thread {
        private Person a;

        public ThreadDemo(Person a) {
            this.a = a;
        }

        @Override
        public void run() {
            while (a.age < 15) {
                a.age++;
                System.out.println(a.age);
            }
        }
    }

    static class ThreadDemo2 extends Thread {
        private Person a;

        public ThreadDemo2(Person a) {
            this.a = a;
        }

        @Override
        public void run() {

            while (true) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("我在执行。。。");
                if (a.age == 10) {
                    System.out.println("hello world");
                }
            }
        }
    }


    public static void method4() {
        LinkedBlockingDeque<Integer> linkedBlockingDeque = new LinkedBlockingDeque<>();
        new Thread(() -> {
            int a = 0;
            for (; ; ) {
                try {
                    linkedBlockingDeque.put(a++);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
        new Thread(() -> {
            for (; ; ) {
                try {
                    Integer take = linkedBlockingDeque.take();
                    System.out.println(take);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }


    public static void method5() throws ExecutionException, InterruptedException {

    }

}
